(function ($) {
	var perId=""
	var startDate = "",endDate="",today="";
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup();
	}
	//获取总工时
	function getTotalHour(){
		//plusFlag 是否计算加减班 0:不计算 1:计算
		var pdict = {'perId':perId,'stDate':startDate,'endDate':endDate,'plusFlag':1,'appFlag':1,'appRet':""}
		var paramDict = {'method':'web.INMArgComm:GetPerHour','params':JSON.stringify(pdict),'type':'MethodNoFilter'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result['data']
			if (!dataDict) {
				mui.toast(result['msg'])
				$('#totalNum')[0].innerText = ''
				return
			}
			$('#totalNum')[0].innerText = (dataDict['Hours']||"")+'小时'
		}, function(errorStr) {
			$('#totalNum')[0].innerText = ''
		});
	}
	//获取加减班 plusFlag 0:加班 1:减班
	function getOverReduceHour(plusFlag){
		var domId = 'overNum'
		if (plusFlag == 1) {
			domId = 'reduceNum'
		}
		var pdict = {'perId':perId,'stDate':startDate,'endDate':endDate,'plusFlag':plusFlag,'appFlag':1,'appRet':""}
		var paramDict = {'method':'web.INMArgComm:GetPerPlusHour','params':JSON.stringify(pdict),'type':'MethodNoFilter'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result['data']
			if (!dataDict) {
				mui.toast(result['msg'])
				$('#'+domId)[0].innerText = ''
				return
			}
			$('#'+domId)[0].innerText = (dataDict['Hours']||"")+'小时'
		}, function(errorStr) {
			$('#'+domId)[0].innerText = ''
		});
	}
	//累计欠存
	function getLeiJiHour(){
		var pdict = {'perId':perId,'date':today,'appFlag':1,'appRet':""}
		var paramDict = {'method':'web.INMArgComm:GetPerProHour','params':JSON.stringify(pdict),'type':'MethodNoFilter'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataDict = result['data']
			if (!dataDict) {
				mui.toast(result['msg'])
				$('#leiji')[0].innerText = ''
				return
			}
			$('#leiji')[0].innerText = (dataDict['Hours']||"")+'小时'
		}, function(errorStr) {
			$('#leiji')[0].innerText = ''
		});
	}
	
	function getDetailBodyData(){
		var pdict = {'perId':perId,'stDate':startDate,'endDate':endDate,'appFlag':1,'appRet':""}
		var paramDict = {'method':'web.INMArgComm:GetPerArgCount','params':JSON.stringify(pdict),'type':'MethodNoFilter'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				$('#detailBody')[0].innerHTML = ''
				showNullTip(result['msg'])
				return
			}
			var dataArr = result['data']
			updateDetailBodyUI(dataArr)
		}, function(errorStr) {
			$('#detailBody')[0].innerHTML = ''
			showNullTip('errorStr')
		});
		
	}
	function updateDetailBodyUI(dataArr){
		var detailBody = $('#detailBody')[0]
		var html = ''
		mui.each(dataArr,function (index,oneDict) {
			html += '<tr><td>'+oneDict['PostDesc']+'</td><td>'+oneDict['Count']+'</td><td>'+oneDict['Hours']+'</td></tr>'
		})
		detailBody.innerHTML = html
		if (html == '') {
			showNullTip('暂无数据')
		}else{
			hideNullTip()
		}
	}

	function selectDate() {
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			// _self.innerText = dateStr;
			refreshPageData(_self,dateStr)
		}, function(e) {}, {});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		perId = self.perId
		today = getCurrentDeviceDay()
		//var self = plus.webview.currentWebview()
		var currYM = self.currYM
		startDate = currYM+'-01'
		$('#startDate')[0].innerHTML = startDate
		var year = parseInt(currYM)
		var month = parseInt(currYM.substring(6,8))
		endDate = currYM+'-'+getMonthMaxDay(year,month)
		$('#endDate')[0].innerHTML = endDate
		mui('body').on('tap','.dateBtn',selectDate)
		getLeiJiHour() //积假
		refreshPageData()
	}
	function refreshPageData(dom,text){
		if (dom) {
			var dateDom = dom.querySelector('div')
			if (dateDom.id == 'endDate') {
				endDate = text
			}else{
				startDate = text
			}
			dateDom.innerText = text
			if (startDate > endDate) {
				return
			}
		}
		getTotalHour() //总工时
		getOverReduceHour(0) //加班
		getOverReduceHour(1) //减班
		getDetailBodyData() //列表部分
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		mui('.nullData')[0].style.display = 'none';
	}
})(mui)
